<?php
include '../../init.php';

include DIR_CORE.'mysql.php';

//var_dump($_POST);

//接受数据
$user = s($_POST['name']);
$password = s($_POST['password']);
//$repassword = s($_POST['repassword']);
$time = time();
$m_password = md5($password);
//判断用户名是否存在

$sql="select * from user where name= '$user'";
$res = mysqli_query($link, $sql);

//判断数据合法性(前端已验证)
/*
if(empty($user)||empty($password)){
    echo '用户名和密码不能为空！';
}
elseif($password!=$repassword){
    echo '两次密码不一致';
}
elseif(strlen($password)<4 || strlen($password)>12){
    echo '密码必须在4-12之间';
}
elseif(strlen($user)<4 || strlen($user)>16)
{
    echo '用户名必须在4-16之间';
}

elseif(mysqli_num_rows($link,$res)>0){
    echo '用户名已存在！';
}else{
*/
    if (mysqli_num_rows($link, $res) > 0) {
        $data = array('status'=>3,'message'=>'用户名已存在！');
        echo $data;
    } else {
        //数据入库
        $sql = "insert into user values(null,'$user','$m_password','$time',default,2,default,default,default,default,default)";
        $result = mysqli_query($link, $sql);
        if ($result) {
            $data = array('status'=>1,'message'=>'添加成功</span>');
            echo json_encode($data);
        } else {
            $data = array('status'=>2,'message'=>'添加失败');
            echo json_encode($data);
        }
    }
